using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using RealState.BusinessObjects;

namespace RealState.DAL
{
	public class UsersDAO
	{
		public UsersDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Users> GetUserss()
		{
			try
			{
				List<Users> lstUserss = new List<Users>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTUserss",CommandType.StoredProcedure);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Users oUsers = new Users();
					oUsers.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oUsers.UserName = Convert.ToString(oDbDataReader["UserName"]);
					oUsers.Password = Convert.ToString(oDbDataReader["Password"]);
					oUsers.FullName = Convert.ToString(oDbDataReader["FullName"]);

					if(oDbDataReader["Tel"] != DBNull.Value)
						oUsers.Tel = Convert.ToString(oDbDataReader["Tel"]);

					if(oDbDataReader["Mobile"] != DBNull.Value)
						oUsers.Mobile = Convert.ToString(oDbDataReader["Mobile"]);

					if(oDbDataReader["Address"] != DBNull.Value)
						oUsers.Address = Convert.ToString(oDbDataReader["Address"]);

					if(oDbDataReader["UserType"] != DBNull.Value)
						oUsers.UserType = Convert.ToInt32(oDbDataReader["UserType"]);
					lstUserss.Add(oUsers);
				}
				oDbDataReader.Close();
				return lstUserss;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Users GetUsers(int Id)
		{
			try
			{
				Users oUsers = new Users();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECTUsers",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oUsers.Id = Convert.ToInt32(oDbDataReader["Id"]);
					oUsers.UserName = Convert.ToString(oDbDataReader["UserName"]);
					oUsers.Password = Convert.ToString(oDbDataReader["Password"]);
					oUsers.FullName = Convert.ToString(oDbDataReader["FullName"]);

					if(oDbDataReader["Tel"] != DBNull.Value)
						oUsers.Tel = Convert.ToString(oDbDataReader["Tel"]);

					if(oDbDataReader["Mobile"] != DBNull.Value)
						oUsers.Mobile = Convert.ToString(oDbDataReader["Mobile"]);

					if(oDbDataReader["Address"] != DBNull.Value)
						oUsers.Address = Convert.ToString(oDbDataReader["Address"]);

					if(oDbDataReader["UserType"] != DBNull.Value)
						oUsers.UserType = Convert.ToInt32(oDbDataReader["UserType"]);
				}
				oDbDataReader.Close();
				return oUsers;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddUsers(Users oUsers)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERTUsers",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,oUsers.UserName));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,oUsers.Password));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@FullName",DbType.String,oUsers.FullName));
				if (oUsers.Tel!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Tel",DbType.String,oUsers.Tel));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Tel",DbType.String,DBNull.Value));
				if (oUsers.Mobile!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Mobile",DbType.String,oUsers.Mobile));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Mobile",DbType.String,DBNull.Value));
				if (oUsers.Address!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Address",DbType.String,oUsers.Address));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Address",DbType.String,DBNull.Value));
				if (oUsers.UserType.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserType",DbType.Int32,oUsers.UserType));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserType",DbType.Int32,DBNull.Value));
				return Convert.ToInt32(DbProviderHelper.ExecuteScalar(oDbCommand));
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateUsers(Users oUsers)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEUsers",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@FullName",DbType.String,oUsers.FullName));
				if (oUsers.Tel!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Tel",DbType.String,oUsers.Tel));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Tel",DbType.String,DBNull.Value));
				if (oUsers.Mobile!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Mobile",DbType.String,oUsers.Mobile));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Mobile",DbType.String,DBNull.Value));
				if (oUsers.Address!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Address",DbType.String,oUsers.Address));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Address",DbType.String,DBNull.Value));
				if (oUsers.UserType.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserType",DbType.Int32,oUsers.UserType));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserType",DbType.Int32,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oUsers.Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
        public int UpdateUsers_2(Users oUsers)
        {

            try
            {
                DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATEUsers_2", CommandType.StoredProcedure);
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password", DbType.String, oUsers.Password));
                oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id", DbType.Int32, oUsers.Id));
                return DbProviderHelper.ExecuteNonQuery(oDbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
		public int RemoveUsers(Users oUsers)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEUsers",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,oUsers.Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveUsers(int Id)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETEUsers",CommandType.StoredProcedure);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Id",DbType.Int32,Id));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
